//
//  LCAFNetWorking.m
//  SugarPile
//
//  Created by Charlie. on 16/1/19.
//  Copyright © 2016年 www.lanou39.蓝鸥科技. All rights reserved.
//

#import "LCAFNetWorking.h"

#import "AppDelegate.h"
#import "MBProgressHUD.h"
#import "NSString+MD5.h"

@implementation LCAFNetWorking

+(void)GETWithURL:(NSString *)url completion:(NETBLOCK)netBlock
{
    AppDelegate *app = [[UIApplication sharedApplication] delegate];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:app.window animated:YES];//菊花
    hud.label.text = NSLocalizedString(@"加载中...", @"HUD loading title");
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", nil];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"Request Successed!");
        id result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        
        netBlock(result);
        
            dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
                sleep(1);
                dispatch_async(dispatch_get_main_queue(), ^{
                    [hud setHidden:YES];//隐藏菊花
                });
            });// 消失

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"Request Failed");
    }];
}

+ (void)POSTWithURL:(NSString *)url body:(NSDictionary *)bodyDic completion:(NETBLOCK)netBlock
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", nil];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url parameters:bodyDic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"Request Successed!");
        id result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        netBlock(result);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"Request Failed");
    }];
}

@end
